fn main() {
    println!("-----------------------");
    test1_ref_as_var();
    println!("-----------------------");
    test2_change_var();
    println!("-----------------------");
    test3_mut_ref();
    println!("-----------------------");
    test4_dangling_ref();
    println!("-----------------------");
}
fn test1_ref_as_var() {
    let s1 = String::from("hello");
    let (s2, len) = calculate_len(&s1);
    println!("len of {s2} is {len}");
}
fn calculate_len(str: &String) -> (String, usize) {
    (str.clone(), str.len())
}
fn test2_change_var() {
    let s1 = String::from("hello");
    change(&s1);
}
fn change(str: &String) {
    //str.push_str(",world");//err
    println!("{str}");
}
fn test3_mut_ref() {
    let mut s1 = String::from("hello");
    change_mut(&mut s1);

    let r1 = &mut s1;
    //let r2 = &mut s1;//err
    //println!("{r1},{r2}");
    println!("{r1}");

    let r1 = &s1;
    let r2 = &s1;
    //let r3 = &mut s1;//err
    println!("{r1},{r2}");
    //println!("{r1},{r2},{r3}");//err

    let r1 = &s1;
    let r2 = &s1;
    println!("{r1},{r2}"); //r1 and r2 is not used
    let r3 = &mut s1; //ok
    println!("{r3}"); //ok
}
fn change_mut(str: &mut String) {
    str.push_str(",world"); //ok
    println!("{str}");
}
fn test4_dangling_ref() {
    let ref_to_none = dangle();
    println!("{ref_to_none}");
}
//fn dangle() -> &String {//err
fn dangle() -> String {
    let s1 = String::from("hello");
    //&s1//err
    s1
}
